package com.danceforyou.ossFile.service.impl;

import com.danceforyou.common.restful.exception.BizException;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AliOssUtil;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.ossFile.dao.OssFileDAO;
import com.danceforyou.ossFile.model.OssFileDO;
import com.danceforyou.ossFile.service.OssFileService;
import com.danceforyou.ossFile.vo.OssFileVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
* 类名称：OssFileServiceImpl
* 创建人：danceforyou
* 创建时间：2021年02月01日
* 版本号：1.0v
*/
@Service("ossFileService")
public class OssFileServiceImpl extends BaseServiceImpl<Long, OssFileDO, OssFileVO, OssFileDAO> implements OssFileService {
    @Autowired
    private OssFileDAO ossFileDAO;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public OssFileDAO getDao() {
        return ossFileDAO;
    }

    @Override
    public OssFileDO saveInfo(MultipartFile file) {
        Date uploadDateTime = new Date();
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        // 解析文件
        String originalFilename = file.getOriginalFilename();
        BigDecimal fileSize = new BigDecimal(file.getSize()).divide(new BigDecimal(1024*1024), 2, BigDecimal.ROUND_UP);
        AssertUtil.isTrue(fileSize.compareTo(BigDecimal.TEN) <= 0, "单个文件大小不能超过10M");
        // 保存云文件
        OssFileDO ossFile = new OssFileDO();
        ossFile.setFileName(originalFilename.substring(0, originalFilename.lastIndexOf(".")));
        if (ossFile.getFileName().length() > 20) {
            ossFile.setFileName(ossFile.getFileName().substring(0, 20));
        }
        ossFile.setFileType(originalFilename.substring(originalFilename.lastIndexOf(".")+1).toLowerCase());
        ossFile.setFileSize(fileSize);
        ossFile.setFileKey(Constants.PROJECT_NAME +"/upload_file/"+ new SimpleDateFormat("yyyyMM").format(uploadDateTime) +"/"+ UUID.randomUUID().toString().replaceAll("-", "") +"."+ ossFile.getFileType());
        ossFile.setUploadDate(new SimpleDateFormat("yyyyMMdd").format(uploadDateTime));
        ossFile.setUploadTime(new SimpleDateFormat("HHmmss").format(uploadDateTime));
        ossFile.setUploadUserId(loginUser.getId());
        try {
            AssertUtil.isTrue(AliOssUtil.putObject(ossFile.getFileKey(), file.getBytes()), "上传文件失败，请联系技术人员");
        } catch (IOException e) {
            throw new BizException("解析文件异常");
        }
        this.insert(ossFile);
        return ossFile;
    }
}
